home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / DAKEY.C < prev    next >
C/C++ Source or Header  |  1995-08-03  |  5KB  |  153 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    dakey.c
  5. //   Title:    Data File Build Utility Library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to manage and create sort keys.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <da.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //   Description:    
  49. //    Parameters:    pcfg            Configuration file information
  50. //       Returns:    
  51. //----------------------------------------------------------------------------
  52. SIZET FN_E DataField(PDATACFG pcfg, PCSZ pcsz)
  53. {
  54.     SIZET i;
  55.  
  56.     Assert(pcfg);
  57.     Assert(pcsz && pcsz[0]);
  58.     for (i = 0; i < pcfg->cFields; ++i)
  59.        if (stricmp(pcfg->afld[i].szName, pcsz) == 0)
  60.         return i;
  61.  
  62.     Halt("Invalid field name!");
  63.     return 0;
  64. }
  65.  
  66.  
  67. //----------------------------------------------------------------------------
  68. //   Description:    Create the sort key for a particular record.
  69. //    Parameters:    pdlmrec        Delimited file data
  70. //            psz            Buffer for key.
  71. //                                        Assumed to be of sufficient size.
  72. //                                        This buffer is NOT null terminated at exit.
  73. //       Returns:    TRUE if successful.
  74. //----------------------------------------------------------------------------
  75. VOID FN_E DataKey(PDLMREC pdlmrec, PSZ psz)
  76. {
  77.     SIZET cKeys = pdlmrec->pcfg->cKeys;
  78.     PDATAFLD pfld = pdlmrec->pcfg->afld;
  79.     PSIZET pcKey = pdlmrec->pcfg->acKey;
  80.     SIZET i, j;
  81.     SIZET cLen;
  82.     PSZ pszKey;
  83.  
  84.     for (i = 0; i < cKeys; ++i)
  85.         {
  86.         BOOL fAlpha = TRUE;
  87.  
  88.         j = pcKey[i];                            // Get data about key
  89.         cLen = pfld[j].cSort ? pfld[j].cSort: pfld[j].cLen;
  90.         pszKey = pdlmrec->apsz[j];
  91.  
  92.         memset(psz, 0, cLen);                // Start by blanking field
  93.         if (pfld[j].fs & FLD_NUMERIC)        // Numeric field -- right justify
  94.             {                                        // Verify field is all numeric
  95.             SIZET cDig, cLead = 0;
  96.                                                     // Dashes signify leading zeroes
  97.             for (cLead = 0; pszKey[cLead] == '-'; cLead++)
  98.                 ;
  99.  
  100.            for (cDig = cLead; pszKey[cDig]; ++cDig)
  101.                if (!isascii(pszKey[cDig]) || !isdigit(pszKey[cDig]))
  102.                     break;
  103.  
  104.             if (cDig)                            // Copy into buffer -- left justified
  105.                 {                                    // Only use numeric portion in bcd format
  106.                 Assert(cLen > 1);
  107.                 cDig = MIN(cLen * 2, cDig - cLead);
  108.                 if (cDig)
  109.                     stra2b((PBYTE)psz, cLen, pszKey + cLead, cDig);
  110.                 fAlpha = FALSE;
  111.                 }    
  112.             }
  113.         if (fAlpha)
  114.             {
  115.             SIZET cStr;
  116.  
  117.             if (BTEST(pfld[j].fs, FLD_FIXED))
  118.                 cStr = MIN(cLen, pfld[j].cLen);
  119.             else
  120.                 cStr = strlen(pszKey);        // Copy alpha, left justified
  121.  
  122.             cStr = MIN(cLen, cStr);
  123.             if (cStr)
  124.                 memcpy(psz, pszKey, cStr);
  125.             }
  126.         psz += cLen;                            // Move to start of next field
  127.         }
  128.     return ;
  129. }
  130.  
  131.  
  132. //----------------------------------------------------------------------------
  133. //   Description:    Compute the size of the current sort key.
  134. //    Parameters:    pcfg            Configuration file information
  135. //       Returns:    Size of key.
  136. //----------------------------------------------------------------------------
  137. SIZET FN_E DataKeyLen(PDATACFG pcfg)
  138. {
  139.     PDATAFLD pfld = pcfg->afld;
  140.     PSIZET pcKey = pcfg->acKey;
  141.     SIZET cKeys = pcfg->cKeys;
  142.     SIZET i;
  143.     SIZET cSize = 0;
  144.  
  145.     for (i = 0; i < cKeys; ++i)
  146.         cSize += pfld[pcKey[i]].cSort ? pfld[pcKey[i]].cSort: pfld[pcKey[i]].cLen;
  147.  
  148.     return cSize;
  149. }
  150. //----------------------------------------------------------------------------
  151. //------------------------------- End of File --------------------------------
  152. //----------------------------------------------------------------------------
  153.